//-当前区-
var m_oRegMark = null;

//-批量添加点-
function AddPoiBat(oPtSrc, nOffsetX, nOffsetY, nCountX, nCountY, bAddPoi)
{
	var i,j;
	var oPt, oItem;
	var oMark;
	var oMarks;
	if (bAddPoi)
	{
		if(m_oAllMarks && m_oAllMarks.length>0)
			m_oMap.removeOverlays(m_oAllMarks);
		//m_oMap.removeAllOverlays();
		oMarks = new Array();
	}
	
	for(i=0; i<nCountX; i++)
	{
		for(j=0; j<nCountY; j++)
		{
			oPt = m_oMap.getLngLatByOffset(oPtSrc,nOffsetX*i,nOffsetY*j,6378137);
			oItem = m_oListView.AddListItem();
			oItem.SetText( i+","+j+"="+oPt.lngX+","+oPt.latY);
			
			oMark = AddPoi(oPt, i+"_"+j, false);
			oMarks.push(oMark);
		}
	}
	m_oAllMarks = oMarks;
	if (bAddPoi)
		m_oMap.addOverlays(oMarks,true); 
}

/*---------------------------------------------
				<<范围查找>>
2) 目前指定餐饮服务
---------------------------------------------*/
var	g_strTemplateItem_RangeResult = '<span id="s3{6}" class="p1" title="{4}" cx="{1}" cy="{2}" name="{0}" address="{7}" filtertext="{0}|{7}">{6}. <b><a href="#" onclick="JumpTo(3, {6});">{0}</a></b>({1},{2}){5}{3}</span><hr/>';
function Range_Callback(data)
{
	if(data.error_message != null)
	{
		alert("错误:" + data.error_message);
		return;
	}

	if(data.message!="ok")
	{
		alert("范围查询失败，网络繁忙!");
		return;
	}

	if(data.searchresult.record==0)
	{
		alert("没有找到对应数据!");
		return;
	}
	
	m_oListView.Clear();
	//m_oMap.removeAllOverlays();
	if(m_oAllMarks && m_oAllMarks.length>0)
		m_oMap.removeOverlays(m_oAllMarks);
	var resultCount=data.searchresult.list.length; 

	var obj, oItem;
	var strItem;

	function sGetTel(obj)
	{
		var str = "";
		if(obj.tel!="")
			str = "; 电话:"+obj.tel;
		return str;
	}

	var oMarks = new Array();
	for (var i = 0; i < resultCount; i++) 
	{   
		obj = data.searchresult.list[i];

		oMark = addFandianMark("rangertn_"+i, [obj.x, obj.y], obj.name, obj.address);
		oMarks.push(oMark);

		strItem = g_strTemplateItem_RangeResult.format
		(
			obj.name,	//name
			obj.x,		//CX
			obj.y,		//CY
			"", 
			obj.x+":"+obj.y,
			"<br/>地址:"+obj.address+sGetTel(obj),//Address
			i+1,
			obj.address
		);		
		oItem = m_oListView.AddListItem();		
		oItem.SetText(strItem);
	}
	m_oAllMarks = oMarks;
	m_oMap.addOverlays(oMarks,false); 
}

$(document).ready(function () 
{
	//鼠标交互测距
	$("#spanTestIA_CalcLength").bind("click", function()
	{
		var	option={}; 
		option.hasCircle=true; 
		option.hasPrompt=true; 
		m_oMap.setCurrentMouseTool(MConstants.RULER,option); 
	});

	//根据坐标计算距离(自己算法)
	$("#spanTestDistance").bind("click", function()
	{
		var dlg = new CDialogObj();
		dlg.ShowFile("frmCalcLength.html",{title:"计算两点距离设置",width:460,height:120,fnClose:calclength_callback});

		function calclength_callback(dlg, oFrm, oFrmWin, oFrmDoc)
		{
			var strCoordA	= oFrmDoc.getElementById("txtCoordA").value;
			var strCoordB	= oFrmDoc.getElementById("txtCoordB").value;
			if(strCoordA=="" || strCoordB=="")
			{
				alert("参数错误");
				return false;
			}

			var	oPoint1 = getLngLat(strCoordA);
			var	oPoint2 = getLngLat(strCoordB);
			if(oPoint1==undefined || oPoint2==undefined)
			{
				alert("参数错误");
				return false;
			}
			calcDistance (oPoint1, oPoint2);
			calcDistance1(oPoint1, oPoint2);

			return true;
		}
		
		//-自定义算法计算两点之间距离-
		function calcDistance(oPoint1, oPoint2)
		{			
			var p1 = fromLngLat(oPoint1);
			var p2 = fromLngLat(oPoint2);
			var dis1 = getGreatCircleDistance(p1[0], p1[1], p2[0], p2[1]);
			var dis2 = getFlatternDistance(p1[0], p1[1], p2[0], p2[1]);
			alert("自定义算法1(粗略) = "+dis1 + ",\n自定义算法2(精确) = "+dis2);
		}

		//-MapABC系统算法Ajax计算两点距离-
		function calcDistance1(oPoint1, oPoint2)
		{   
			var opt = new MSpatialSearchOptions();
			var mrs = new MSpatialSearch(); 
				mrs.setCallbackFunction(calcDistance_CallBack);   
				mrs.calculaDistance(getLngLat(oPoint1),getLngLat(oPoint2),null,opt);
		}

		function calcDistance_CallBack(data)
		{   
			if(data.error_message != null)
				alert("查询异常！"+data.error_message); 
			else
				alert("MapABC两点距离："+data.length);   
		}
	});

	function addOverlay_callback(param)
	{
		var oCurMark = m_oMap.getOverlayById(param.overlayId);
		if(	oCurMark.TYPE!=MOverlay.TYPE_POLYGON && 
			oCurMark.TYPE!=MOverlay.TYPE_RECTANGLE && 
			oCurMark.TYPE!=MOverlay.TYPE_CIRCLE &&
			oCurMark.TYPE!=MOverlay.TYPE_AREA)
			return;

		if(m_oRegMark)
		{
			m_oMap.removeOverlay(m_oRegMark);
			m_oRegMark = null;
		}

		m_oRegMark = oCurMark;
	}
	if(m_oMap)
		m_oMap.addEventListener(m_oMap,MConstants.ADD_OVERLAY, addOverlay_callback);

	//鼠标交互画矩形
	$("#spanTestIA_Rect").bind("click", function()
	{
		m_oMap.setCurrentMouseTool(MConstants.DRAW_RECTANGLE);
		m_oMap.addEventListener(m_oMap,MConstants.ADD_OVERLAY, addOverlay_callback);
	});

	//鼠标交互画区
	$("#spanTestIA_Reg").bind("click", function()
	{
		m_oMap.setCurrentMouseTool(MConstants.DRAW_POLYGON);
		m_oMap.addEventListener(m_oMap,MConstants.ADD_OVERLAY, addOverlay_callback);
	});

	//由点造区	
	$("#spanTestMakeReg").bind("click", function()
	{		
		var dlg = new CDialogObj();
		dlg.ShowFile("frmCoords.html",{title:"多点造区",width:800,height:380,fnClose:PntToReg_callback});

		function PntToReg_callback(dlg, oFrm, oFrmWin, oFrmDoc)
		{
			var strCoords = oFrmDoc.getElementById("txtCoords").value;
			if(strCoords=="")
			{
				alert("参数错误");
				return false;
			}
						
			var arr = getLngLatArray(strCoords);
			if(!arr || arr.length<2)
			{
				alert("参数错误了!");
				return false;
			}

			var oReg = createPolygon(arr);
			if(m_oRegMark)
			{
				m_oMap.removeOverlay(m_oRegMark);
				m_oRegMark = null;
			}
			m_oMap.addOverlay(oReg, true);	//此时会触发MConstants.MOUSE_CLICK
			m_oRegMark = oReg;

			return true;
		}
	});

	//由点造点	
	$("#spanTestAddPoint").bind("click", function()
	{		
		var dlg = new CDialogObj();
		dlg.ShowFile("frmMulCoords.html",{title:"批量加离散点",width:800,height:380,fnClose:PntToReg_callback});

		function PntToReg_callback(dlg, oFrm, oFrmWin, oFrmDoc)
		{
			var strCoords = oFrmDoc.getElementById("txtCoords").value;
			if(strCoords=="")
			{
				alert("参数错误");
				return false;
			}
						
			sTestAddFandianFromDataString(strCoords);
			return true;
		}
	});
	
	//导出
	$("#spanTestExport").bind("click", function()
	{
		if(!m_oRegMark)
		{
			alert("请先输入区!");
			return;
		}

		var  json=m_oMap.expOverlay(m_oRegMark.id);
		function fnLoad_callback(dlg, oFrm, oFrmWin, oFrmDoc)
		{
			if(oFrmDoc.URL=="about:blank")
				oFrmDoc = oFrm.contentWindow.document;
			oFrmDoc.getElementById("txtJson").value = json2str(json);
			oFrmDoc.getElementById("txtType").value = json.type;
			oFrmDoc.getElementById("txtId").value = json.id;

			var	str="";
			var length=json.coordinates.length; 
			for (i=0;i<length ;i++ )   
			{ 
				str=str+json.coordinates[i].x+","+json.coordinates[i].y+";"; 
			}			
			oFrmDoc.getElementById("txtCoords").value = str;
		}

		var dlg = new CDialogObj();
		dlg.ShowFile("frmMarkInfo.html",{title:"导出信息",width:800,height:400,fnLoad:fnLoad_callback});
	});

	//范围查询
	$("#spanTestRange").bind("click", function()
	{
		if(!m_oRegMark)
		{
			alert("查询之前请先输入区!");
			return;
		}

		var oPts = null;
		var sType = m_oRegMark.TYPE;
		var lnglats;
		var arr;
		if(MOverlay.TYPE_POLYGON==sType)
		{
			lnglats = m_oRegMark.lnglatArr;
			arr = new Array();
			for (var i=0;i<lnglats.length;i++)
			{		　　	
				arr.push(lnglats[i]);
		　　}
			oPts = new MLngLats(arr);
		}
		else if(MOverlay.TYPE_RECTANGLE==sType)
		{
			lnglats = m_oRegMark.lnglatArr;			
			arr = new Array();
			arr.push(lnglats[0]);
			arr.push(new MLngLat(lnglats[1].lngX,lnglats[0].latY));
			arr.push(new MLngLat(lnglats[1].lngX,lnglats[1].latY));
			arr.push(lnglats[1]);

			oPts = new MLngLats(arr);			
		}

		if(oPts)
			RangeSearch(Range_Callback, "", oPts, getCurDataType());
	});

	//批量加点
	$("#spanTestPoint").bind("click", function()
	{
		function batCoordSet_callback(dlg, oFrm, oFrmWin, oFrmDoc)
		{
			var strSet	= oFrmDoc.getElementById("txtCoord").value;
			var nStepX	= parseInt(oFrmDoc.getElementById("txtStepX").value);
			var nStepY	= parseInt(oFrmDoc.getElementById("txtStepY").value);
			var nCountX	= parseInt(oFrmDoc.getElementById("txtCountX").value);
			var nCountY	= parseInt(oFrmDoc.getElementById("txtCountY").value);
			if(strSet=="" || nStepX<=0 || nStepY<=0 || nCountX<=0 || nCountY<=0)
				return false;

			var oPtSrc = getLngLat(strSet);//中心点坐标 
			if(!oPtSrc)
				return false;
			AddPoiBat(oPtSrc, nStepX, nStepY, nCountX, nCountY, true);
			return true;
		}

		var dlg = new CDialogObj();
		dlg.ShowFile("frmBatCoord.html",{title:"批量添加坐标点设置",width:450,height:186,fnClose:batCoordSet_callback});
	});

	//清空叠加层
	$("#spanTestDelAll").bind("click", function()
	{
		m_oMap.removeAllOverlays();
		m_oAllMarks = null;
		m_oRegMark = null;
	});
});